/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QFileDialog>

#include "DictSettingDialog.h"
#include "DictManager.h"
#include "Logger.h"

DictSettingDialog::DictSettingDialog(QDialog *parent)
    : QDialog(parent)
{
	ui.setupUi(this);

	mDictModel = new QStandardItemModel(this);

	QShortcut* closeShortcut = new QShortcut(QKeySequence(Qt::Key_Escape),  this);
	QObject::connect(closeShortcut,SIGNAL(activated()),this,SLOT(OKButtonClicked()));

	ui.dictUpButton->setIcon(QIcon(":/pixmaps/arrow-up.png"));
	ui.dictDownButton->setIcon(QIcon(":/pixmaps/arrow-down.png"));

	QObject::connect(ui.settingOKButton, SIGNAL(clicked()), this, SLOT(OKButtonClicked()));
	QObject::connect(ui.dictUpButton, SIGNAL(clicked()), this, SLOT(dictsMoveUpButtonClicked()));
	QObject::connect(ui.dictDownButton, SIGNAL(clicked()), this, SLOT(dictsMoveDownButtonClicked()));

	ui.dictTable->setModel(mDictModel);
	ui.dictTable->setEditTriggers(false);
	ui.dictTable->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui.dictTable->setAlternatingRowColors(true);
	QObject::connect(ui.dictTable, SIGNAL(clicked(const QModelIndex&)), this, SLOT(dictItemSelected(const QModelIndex&)));

	ui.mainShadingColor->setPalette(QPalette(DICT_OPT.mMainShadingColor));
	ui.mainShadingColor->setAutoFillBackground(true);
	QObject::connect(ui.setMainShadingButton, SIGNAL(clicked()), this, SLOT(showMainShadingColorDialog()));

	ui.selectShadingColor->setPalette(QPalette(DICT_OPT.mSelectShadingColor));
	ui.selectShadingColor->setAutoFillBackground(true);
	QObject::connect(ui.setSelectShadingButton, SIGNAL(clicked()), this, SLOT(showSelectionShadingColorDialog()));

	QObject::connect(ui.useExternalPlayerButton, SIGNAL(toggled(bool)), ui.speakCommandEdit, SLOT(setEnabled(bool)));
	QObject::connect(ui.setSpeakLibButton, SIGNAL(clicked()), this, SLOT(setSoundLibPath()));
}

DictSettingDialog::~DictSettingDialog()
{
}

int DictSettingDialog::exec()
{
	setDictTable();
	ui.useExternalPlayerButton->setChecked(DICT_OPT.useExternalPlayer);
	ui.speakCommandEdit->setText(DICT_OPT.playSoundCommand);
	ui.speakLibraryPathEdit->setText(DICT_OPT.speakLibPath);

	return QDialog::exec();
}

void DictSettingDialog::OKButtonClicked()
{
	updateDictList();
	DICT_OPT.useExternalPlayer = ui.useExternalPlayerButton->isChecked();
	DICT_OPT.playSoundCommand = ui.speakCommandEdit->text();
	DICT_OPT.speakLibPath = ui.speakLibraryPathEdit->text();

	this->accept();
}

void DictSettingDialog::setDictTable()
{
	QStringList queryDictList = DICT_OPT.mQueryDictList;
	QStringList validDictList = DM.getAvailableDictList();

	QStringList headers;
	headers<<QString(tr("Enabled"));
	headers<<QString(tr("Name"));

	mDictModel->clear();
	mDictModel->setHorizontalHeaderLabels(headers);
	mDictModel->setRowCount(0);

	for (int i=0; i<queryDictList.size(); i++)
	{
		QString dictName = queryDictList[i];
		if (!validDictList.contains(dictName))
			continue;

		QStandardItem *item = new QStandardItem();
		item->setSelectable(true);
		item->setCheckable(true);
		item->setCheckState(Qt::Checked);

		QStandardItem *dict = new QStandardItem(dictName);
		dict->setSelectable(true);

		QList<QStandardItem*> dictRow;
		dictRow<<item<<dict;
		mDictModel->insertRow(mDictModel->rowCount(), dictRow);
	}

	for(int i=0; i<validDictList.size(); i++)
	{
		QString dictName = validDictList[i];
		if (!queryDictList.contains(dictName))
		{

			QStandardItem *item = new QStandardItem();
			item->setSelectable(true);
			item->setCheckable(true);
			item->setCheckState(Qt::Unchecked);

			QStandardItem *dict = new QStandardItem(dictName);

			QList<QStandardItem*> dictRow;
			dictRow.push_back(item);
			dictRow.push_back(dict);
			mDictModel->insertRow(mDictModel->rowCount(), dictRow);
		}
	}

	ui.dictTable->resizeColumnsToContents();
}

void DictSettingDialog::dictsMoveUpButtonClicked()
{
    int currentRow = ui.dictTable->currentIndex().row();
    if (currentRow > 0)
    {
    	mDictModel->insertRow(currentRow - 1, mDictModel->takeRow(currentRow));
    	ui.dictTable->selectRow(currentRow - 1);
    }
}

void DictSettingDialog::dictsMoveDownButtonClicked()
{
    int currentRow = ui.dictTable->currentIndex().row();
    if (currentRow < mDictModel->rowCount() - 1)
    {
    	mDictModel->insertRow(currentRow + 1, mDictModel->takeRow(currentRow));
    	ui.dictTable->selectRow(currentRow + 1);
    }
}

void DictSettingDialog::dictItemSelected(const QModelIndex& index)
{
	int row = index.row();
	Qt::CheckState state = mDictModel->item(row, 0)->checkState();
	if (state == Qt::Checked)
		mDictModel->item(row, 0)->setCheckState(Qt::Unchecked);
	else
		mDictModel->item(row, 0)->setCheckState(Qt::Checked);
}

void DictSettingDialog::updateDictList()
{
	DICT_OPT.mQueryDictList.clear();
	for(int i=0; i<mDictModel->rowCount(); i++)
	{
		QStandardItem *item = mDictModel->item(i,0);
		if(item->checkState() == Qt::Checked)
		{
			DICT_OPT.mQueryDictList<<mDictModel->item(i,1)->text();
		}
	}
}

void DictSettingDialog::showMainShadingColorDialog()
{
	QColorDialog dialog;
	dialog.setCurrentColor(DICT_OPT.mMainShadingColor);
	dialog.exec();
	DICT_OPT.mMainShadingColor = dialog.currentColor().name();
	ui.mainShadingColor->setPalette(QPalette(DICT_OPT.mMainShadingColor));
}

void DictSettingDialog::showSelectionShadingColorDialog()
{
	QColorDialog dialog;
	dialog.setCurrentColor(DICT_OPT.mSelectShadingColor);
	dialog.exec();
	DICT_OPT.mSelectShadingColor = dialog.currentColor().name();
	ui.selectShadingColor->setPalette(QPalette(DICT_OPT.mSelectShadingColor));
}

void DictSettingDialog::setSoundLibPath()
{
	QFileDialog dialog(this, QString(tr("Set sound library path")), DICT_OPT.speakLibPath);
	dialog.setFileMode(QFileDialog::Directory);
	dialog.exec();
	if (!dialog.selectedFiles().isEmpty())
	{
		QString dirStr = dialog.selectedFiles().at(0);
		ui.speakLibraryPathEdit->setText(dirStr);
		DICT_OPT.speakLibPath = dirStr;
	}
}
